//
//  AddressNavgationViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/10/9.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "AddressNavgationViewController.h"
#import "JXMapNavigationView.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Search/BMKPoiSearch.h>
#import <BaiduMapAPI_Map/BMKAnnotation.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>
@interface AddressNavgationViewController ()<BMKMapViewDelegate,BMKPoiSearchDelegate,BMKLocationServiceDelegate,UIActionSheetDelegate>
{
    double currentlatitude;
    double currentlongtitude;
    double targetlatitude;
    double targetlongtitude;
}
@property(nonatomic,strong)BMKMapView *mapView;
@property(nonatomic,strong)UIActionSheet *sheet;
@property(nonatomic,strong)BMKLocationService *service;
@property (nonatomic, strong)JXMapNavigationView *mapNavigationView;

@end

@implementation AddressNavgationViewController
- (JXMapNavigationView *)mapNavigationView{
    if (_mapNavigationView == nil) {
        _mapNavigationView = [[JXMapNavigationView alloc]init];
    }
    return _mapNavigationView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.nameStr);
    NSLog(@"%@",self.addressStr);
    [self createBMKMapView];
    // Do any additional setup after loading the view.
}
-(void)createBMKMapView{
    sleep(0.7);
    self.mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.delegate = self;
    [self.mapView setMapType:BMKMapTypeStandard];
    //    self.mapView.trafficEnabled = YES;
    self.mapView.showMapPoi = YES;
    self.mapView.zoomLevel = 21;
    self.mapView.rotateEnabled = NO;
    self.mapView.scrollEnabled = YES;
    self.mapView.showMapScaleBar = YES;
    
//    self.mapView setRegion:<#(BMKCoordinateRegion)#>
    [self.view addSubview:self.mapView];
    [self ForwardCode:self.addressStr];
    self.service = [[BMKLocationService alloc] init];
    self.service.delegate = self;
    [self.service startUserLocationService];
}

//正向地理编码
-(void)ForwardCode:(NSString *)str{
    //编码对象
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    //正向地理编码
    [geoCoder geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count == 0) {
            NSLog(@"编码失败");
            return ;
        }
        CLPlacemark *placeMark = [placemarks lastObject];
        targetlatitude = placeMark.location.coordinate.latitude;
        targetlongtitude = placeMark.location.coordinate.longitude;
        NSLog(@"%lf--%lf",placeMark.location.coordinate.latitude,placeMark.location.coordinate.longitude);
        //初始化一个点的注释 //只有三个属性
        BMKPointAnnotation *annotoation = [[BMKPointAnnotation alloc] init];
        //坐标
        annotoation.coordinate = placeMark.location.coordinate;
        //title
        annotoation.title = @"123";
        NSLog(@"这是你想要的名字%@",annotoation.title);
        //子标题
        annotoation.subtitle = @"34324";
        //将标注添加到地图上
        [self.mapView addAnnotation:annotoation];
        //根据用户位置设置地图的中心点
        [_mapView setCenterCoordinate:placeMark.location.coordinate animated:YES];
        //设置显示范围
        BMKCoordinateRegion region;
        region.span.latitudeDelta = 0.01;
        region.span.longitudeDelta = 0.01;
        region.center = placeMark.location.coordinate;
        [self.mapView setRegion:region];
    }];
    
}




/**
 大头针的代理方法

 */
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        //        根据注视点创建标注
//        BMKPinAnnotationView *newAnnotion = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
//        newAnnotion.pinColor = BMKPinAnnotationColorRed;
//        newAnnotion.animatesDrop = YES;
//        NSLog(@"走了么");
//        return newAnnotion;
    
        
        
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"customAnnotaion"];
        newAnnotationView.animatesDrop = YES;
        newAnnotationView.annotation = annotation;
        UIView *areaPaoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 60)];
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.text = self.nameStr;
        [areaPaoView addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(areaPaoView.mas_left).offset(5);
            make.top.equalTo(areaPaoView.mas_top).offset(5);
            make.size.mas_equalTo(CGSizeMake(180, 20));
        }];
        UILabel *addressLabel = [[UILabel alloc] init];
        addressLabel.text = self.addressStr;
        [areaPaoView addSubview:addressLabel];
        [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(180, 20));
            make.left.equalTo(areaPaoView.mas_left).offset(5);
            make.bottom.equalTo(areaPaoView.mas_bottom).offset(-5);
        }];
        UIButton *navBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        navBtn.backgroundColor = [UIColor orangeColor];
        [navBtn setTitle:@"导航" forState:UIControlStateNormal];
        [navBtn setImage:[UIImage imageNamed:@"地图导航"] forState:UIControlStateNormal];
        [navBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [navBtn addTarget:self action:@selector(showMapChoose:) forControlEvents:UIControlEventTouchUpInside];
        [areaPaoView addSubview:navBtn];
        [navBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.top.equalTo(areaPaoView);
            make.width.mas_equalTo(80);
        }];
        areaPaoView.backgroundColor = [UIColor whiteColor];
        BMKActionPaopaoView *paopaoView = [[BMKActionPaopaoView alloc] initWithCustomView:areaPaoView];
        newAnnotationView.paopaoView = paopaoView;
       
        return newAnnotationView;
    }
    return nil;
}
-(void)showMapChoose:(UIButton *)btn{
    NSLog(@"当前的位置%lf----%lf",currentlatitude,currentlongtitude);
    [self.mapNavigationView showMapNavigationViewFormcurrentLatitude:currentlatitude currentLongitute:currentlongtitude TotargetLatitude:targetlatitude targetLongitute:targetlongtitude toName:self.nameStr];

}

/**
 检查手机是否含有地图

 */
-(NSArray *)checkHasOwnApp{
    NSArray *mapSchemeArr = @[@"comgooglemaps://",@"iosamap://navi",@"baidumap://map/",@"qqmap://"];
    
    NSMutableArray *appListArr = [[NSMutableArray alloc] initWithObjects:@"苹果原生地图", nil];
    
    for (int i = 0; i < [mapSchemeArr count]; i++) {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[mapSchemeArr objectAtIndex:i]]]]) {
            if (i == 0) {
                [appListArr addObject:@"google地图"];
            }else if (i == 1){
                [appListArr addObject:@"高德地图"];
            }else if (i == 2){
                [appListArr addObject:@"百度地图"];
            }else if (i == 3){
                [appListArr addObject:@"腾讯地图"];
            }
        }
    }
    
    return appListArr;
}

-(void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *) [views objectAtIndex:0];
    [mapView selectAnnotation:annotationView.annotation animated:YES];
}
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    currentlatitude = userLocation.location.coordinate.latitude;
    currentlongtitude = userLocation.location.coordinate.longitude;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil;
    self.service.delegate = nil;
}
-(void)dealloc{
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil;
    self.service.delegate = nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
